package com.msteam.tools.code.controller;

import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.msteam.kernel.render.vo.GridGson;
import com.msteam.kernel.render.vo.ResponseJson;

import com.msteam.tools.code.generator.CubeHandGenerator;
import com.msteam.tools.code.model.*;
import com.msteam.tools.code.model.json.*;
import com.msteam.tools.report.model.*;
import com.msteam.tools.report.model.json.*;

import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@RouterMapping(url = "/code/api", viewPath = "/WEB-INF/api")
public class ApiController extends Controller {
	private Gson gson = new Gson();
	

	/**
	 * 从数据库更新表结构
	 */
	public void refresh(){
		Integer projectId = this.getParaToInt("id");
		CbhProject project = new CbhProject().findById(projectId);
		CubeHandGenerator gen = new CubeHandGenerator(project);
		gen.refreshProjectTableColumns();
		/*//创建表生成器
		JShopExMetaBuilder mmbuilder = new JShopExMetaBuilder(getDataSource(project));
		//添加需要生成的表前缀
		mmbuilder.setTablePrefix(project.getTablePrefix());
		*/
		
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	
	/**
	 * 生成代码
	 */
	public void build(){
		Integer projectId = this.getParaToInt("id");
		CbhProject project = new CbhProject().findById(projectId);
		CubeHandGenerator gen = new CubeHandGenerator(project);
		//gen.refreshTemplate();
		gen.start();
		
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	
	private List<CbhColumnJson> _wrapCbhColumnJson(List<CbhColumn> cbhcolumns){
		List<CbhColumnJson> jsonItems = new ArrayList<CbhColumnJson>();
		for(CbhColumn gt:cbhcolumns){
			jsonItems.add(_wrapCbhColumnJsonItem(gt));
		}
		return jsonItems;
	}

	private CbhColumnJson _wrapCbhColumnJsonItem(CbhColumn objItem){
		List<CbhColumnJson> jsonItems = new ArrayList<CbhColumnJson>();
		
			CbhColumnJson jsonItem = new CbhColumnJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setTABLE_ID(objItem.getTableId());
			jsonItem.setNAME(objItem.getNAME());
			jsonItem.setDISPLAY_NAME(objItem.getDisplayName());
			jsonItem.setJAVA_TYPE(objItem.getJavaType());
			jsonItem.setATTR_NAME(objItem.getAttrName());
			jsonItem.setTYPE(objItem.getTYPE());
			jsonItem.setSIZE(objItem.getSize());
			jsonItem.setIS_NULL_ABLE(objItem.getIsNullAble());
			jsonItem.setIS_PRIMARY_KEY(objItem.getIsPrimaryKey());
			jsonItem.setDEFAULT_VALUE(objItem.getDefaultValue());
			jsonItem.setREMARKS(objItem.getREMARKS());
			jsonItem.setQUERY_BY(objItem.getQueryBy());
			jsonItem.setORDER_BY(objItem.getOrderBy());
			jsonItem.setIN_LIST(objItem.getInList());
			jsonItem.setIN_EDIT(objItem.getInEdit());
			jsonItem.setIN_VIEW(objItem.getInView());
			jsonItem.setFRONT_IN_LIST(objItem.getFrontInList());
			jsonItem.setFRONT_IN_EDIT(objItem.getFrontInEdit());
			jsonItem.setFRONT_IN_VIEW(objItem.getFrontInView());
			jsonItem.setIS_APPLY(objItem.getIsApply());
			jsonItem.setINPUT_TYPE(objItem.getInputType());
		
			jsonItem.setEXTRA_INFO(objItem.getExtraInfo());
			
		return jsonItem;
	}
	
	public void cbhcolumns(){
		GridGson gg = new GridGson();
		CbhColumn t = new CbhColumn();
		List<CbhColumn> items = t.find("select * from cbh_column where TABLE_ID=? ",this.getParaToInt("id"));
		gg.setTotal(items.size());
		gg.setRows(_wrapCbhColumnJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewcbhcolumnJson(){
		
		CbhColumn item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new CbhColumn();
		}else{
			Integer id = this.getParaToInt("id");
			item = new CbhColumn().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapCbhColumnJsonItem(item)));
	}
	
	public void updatecbhcolumn(){
		Map t = getRequestObject(HashMap.class);
		CbhColumn item = null;
		if(t.containsKey("ID")){
			item = new CbhColumn().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new CbhColumn();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<CbhProjectJson> _wrapCbhProjectJson(List<CbhProject> cbhprojects){
		List<CbhProjectJson> jsonItems = new ArrayList<CbhProjectJson>();
		for(CbhProject gt:cbhprojects){
			jsonItems.add(_wrapCbhProjectJsonItem(gt));
		}
		return jsonItems;
	}

	private CbhProjectJson _wrapCbhProjectJsonItem(CbhProject objItem){
		List<CbhProjectJson> jsonItems = new ArrayList<CbhProjectJson>();
		
			CbhProjectJson jsonItem = new CbhProjectJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setNAME(objItem.getNAME());
			jsonItem.setTEMPLATE_ID(objItem.getTemplateId());
			jsonItem.setVERSION(objItem.getVERSION());
			jsonItem.setDESCRIPTION(objItem.getDESCRIPTION());
			jsonItem.setAUTHOR(objItem.getAUTHOR());
			jsonItem.setCREATE_DATE(objItem.getCreateDate());
			jsonItem.setVERSION_DATE(objItem.getVersionDate());
			jsonItem.setTABLE_PREFIX(objItem.getTablePrefix());
			jsonItem.setDB_HOST(objItem.getDbHost());
			jsonItem.setDB_NAME(objItem.getDbName());
			jsonItem.setDB_USER(objItem.getDbUser());
			jsonItem.setDB_PASSWORD(objItem.getDbPassword());
			jsonItem.setEXTRA_INFO(objItem.getExtraInfo());
		return jsonItem;
	}
	
	public void cbhprojects(){
		GridGson gg = new GridGson();
		CbhProject t = new CbhProject();
		List<CbhProject> items = t.find("select * from cbh_project ");
		gg.setTotal(items.size());
		gg.setRows(_wrapCbhProjectJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewcbhprojectJson(){
		
		CbhProject item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new CbhProject();
		}else{
			Integer id = this.getParaToInt("id");
			item = new CbhProject().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapCbhProjectJsonItem(item)));
	}
	
	public void updatecbhproject(){
		Map t = getRequestObject(HashMap.class);
		CbhProject item = null;
		if(t.containsKey("ID") && Double.parseDouble(t.get("ID").toString())>0){
			item = new CbhProject().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new CbhProject();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<CbhTableJson> _wrapCbhTableJson(List<CbhTable> cbhtables){
		List<CbhTableJson> jsonItems = new ArrayList<CbhTableJson>();
		for(CbhTable gt:cbhtables){
			jsonItems.add(_wrapCbhTableJsonItem(gt));
		}
		return jsonItems;
	}

	private CbhTableJson _wrapCbhTableJsonItem(CbhTable objItem){
		List<CbhTableJson> jsonItems = new ArrayList<CbhTableJson>();
		
			CbhTableJson jsonItem = new CbhTableJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setPROJECT_ID(objItem.getProjectId());
			jsonItem.setPREFIX(objItem.getPREFIX());
			jsonItem.setTABLE_NAME(objItem.getTableName());
			jsonItem.setDISPLAY_NAME(objItem.getDisplayName());
			jsonItem.setMODEL_NAME(objItem.getModelName());
			jsonItem.setPRIMARY_KEY(objItem.getPrimaryKey());
			jsonItem.setREMARKS(objItem.getREMARKS());
			jsonItem.setPACKAGE_NAME(objItem.getPackageName());
			jsonItem.setIS_PROCESS(objItem.getIsProcess());
			jsonItem.setEXTRA_INFO(objItem.getExtraInfo());
		return jsonItem;
	}
	
	public void cbhtables(){
		GridGson gg = new GridGson();
		CbhTable t = new CbhTable();
		List<CbhTable> items = t.find("select * from cbh_table where PROJECT_ID=? order by TABLE_NAME asc ",this.getParaToInt("id"));
		gg.setTotal(items.size());
		gg.setRows(_wrapCbhTableJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewcbhtableJson(){
		
		CbhTable item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new CbhTable();
		}else{
			Integer id = this.getParaToInt("id");
			item = new CbhTable().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapCbhTableJsonItem(item)));
	}
	
	public void updatecbhtable(){
		Map t = getRequestObject(HashMap.class);
		CbhTable item = null;
		if(t.containsKey("ID")){
			item = new CbhTable().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new CbhTable();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<CbhTemplateJson> _wrapCbhTemplateJson(List<CbhTemplate> cbhtemplates){
		List<CbhTemplateJson> jsonItems = new ArrayList<CbhTemplateJson>();
		for(CbhTemplate gt:cbhtemplates){
			jsonItems.add(_wrapCbhTemplateJsonItem(gt));
		}
		return jsonItems;
	}

	private CbhTemplateJson _wrapCbhTemplateJsonItem(CbhTemplate objItem){
		List<CbhTemplateJson> jsonItems = new ArrayList<CbhTemplateJson>();
		
			CbhTemplateJson jsonItem = new CbhTemplateJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setNAME(objItem.getNAME());
			jsonItem.setAUTHOR(objItem.getAUTHOR());
			jsonItem.setVERSION(objItem.getVERSION());
			jsonItem.setCODE(objItem.getCODE());
			jsonItem.setCREATE_DATE(objItem.getCreateDate());
		
		return jsonItem;
	}
	
	public void cbhtemplates(){
		GridGson gg = new GridGson();
		CbhTemplate t = new CbhTemplate();
		List<CbhTemplate> items = t.find("select * from cbh_template ");
		gg.setTotal(items.size());
		gg.setRows(_wrapCbhTemplateJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewcbhtemplateJson(){
		
		CbhTemplate item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new CbhTemplate();
		}else{
			Integer id = this.getParaToInt("id");
			item = new CbhTemplate().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapCbhTemplateJsonItem(item)));
	}
	
	public void updatecbhtemplate(){
		Map t = getRequestObject(HashMap.class);
		CbhTemplate item = null;
		if(t.containsKey("ID")){
			item = new CbhTemplate().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new CbhTemplate();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<CbhTemplateFileJson> _wrapCbhTemplateFileJson(List<CbhTemplateFile> cbhtemplatefiles){
		List<CbhTemplateFileJson> jsonItems = new ArrayList<CbhTemplateFileJson>();
		for(CbhTemplateFile gt:cbhtemplatefiles){
			jsonItems.add(_wrapCbhTemplateFileJsonItem(gt));
		}
		return jsonItems;
	}

	private CbhTemplateFileJson _wrapCbhTemplateFileJsonItem(CbhTemplateFile objItem){
		List<CbhTemplateFileJson> jsonItems = new ArrayList<CbhTemplateFileJson>();
		
			CbhTemplateFileJson jsonItem = new CbhTemplateFileJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setTEMPLATE_ID(objItem.getTemplateId());
			jsonItem.setPARENT_ID(objItem.getParentId());
			jsonItem.setFILE_TYPE(objItem.getFileType());
			jsonItem.setNAME(objItem.getNAME());
			jsonItem.setPATH(objItem.getPATH());
			jsonItem.setPROCESS_TYPE(objItem.getProcessType());
			jsonItem.setOUTPUT_PATH(objItem.getOutputPath());
			jsonItem.setFILE_SIZE(objItem.getFileSize());
			jsonItem.setDESCRIPTION(objItem.getDESCRIPTION());
		
		return jsonItem;
	}
	
	public void cbhtemplatefiles(){
		GridGson gg = new GridGson();
		CbhTemplateFile t = new CbhTemplateFile();
		List<CbhTemplateFile> items = t.find("select * from cbh_template_file where TEMPLATE_ID=?",this.getParaToInt("id"));
		gg.setTotal(items.size());
		gg.setRows(_wrapCbhTemplateFileJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewcbhtemplatefileJson(){
		
		CbhTemplateFile item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new CbhTemplateFile();
		}else{
			Integer id = this.getParaToInt("id");
			item = new CbhTemplateFile().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapCbhTemplateFileJsonItem(item)));
	}
	
	public void updatecbhtemplatefile(){
		Map t = getRequestObject(HashMap.class);
		CbhTemplateFile item = null;
		if(t.containsKey("ID")){
			item = new CbhTemplateFile().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new CbhTemplateFile();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	
	private List<RptDatasourceJson> _wrapRptDatasourceJson(List<RptDatasource> rptdatasources){
		List<RptDatasourceJson> jsonItems = new ArrayList<RptDatasourceJson>();
		for(RptDatasource gt:rptdatasources){
			jsonItems.add(_wrapRptDatasourceJsonItem(gt));
		}
		return jsonItems;
	}

	private RptDatasourceJson _wrapRptDatasourceJsonItem(RptDatasource objItem){
		List<RptDatasourceJson> jsonItems = new ArrayList<RptDatasourceJson>();
		
			RptDatasourceJson jsonItem = new RptDatasourceJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setNAME(objItem.getNAME());
			jsonItem.setQUERY_SQL(objItem.getQuerySql());
		
		return jsonItem;
	}
	
	public void rptdatasources(){
		GridGson gg = new GridGson();
		RptDatasource t = new RptDatasource();
		List<RptDatasource> items = t.find("select * from rpt_datasource ");
		gg.setTotal(items.size());
		gg.setRows(_wrapRptDatasourceJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewrptdatasourceJson(){
		
		RptDatasource item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new RptDatasource();
		}else{
			Integer id = this.getParaToInt("id");
			item = new RptDatasource().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapRptDatasourceJsonItem(item)));
	}
	
	public void updaterptdatasource(){
		Map t = getRequestObject(HashMap.class);
		RptDatasource item = null;
		if(t.containsKey("ID")){
			item = new RptDatasource().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new RptDatasource();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<RptDatasourceFilterJson> _wrapRptDatasourceFilterJson(List<RptDatasourceFilter> rptdatasourcefilters){
		List<RptDatasourceFilterJson> jsonItems = new ArrayList<RptDatasourceFilterJson>();
		for(RptDatasourceFilter gt:rptdatasourcefilters){
			jsonItems.add(_wrapRptDatasourceFilterJsonItem(gt));
		}
		return jsonItems;
	}

	private RptDatasourceFilterJson _wrapRptDatasourceFilterJsonItem(RptDatasourceFilter objItem){
		List<RptDatasourceFilterJson> jsonItems = new ArrayList<RptDatasourceFilterJson>();
		
			RptDatasourceFilterJson jsonItem = new RptDatasourceFilterJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setDATASOURCE_ID(objItem.getDatasourceId());
			jsonItem.setFILTER_ID(objItem.getFilterId());
			jsonItem.setVARIABLE(objItem.getVARIABLE());
			jsonItem.setWHERE_SQL(objItem.getWhereSql());
			jsonItem.setDESCRIPTION(objItem.getDESCRIPTION());
		
		return jsonItem;
	}
	
	public void rptdatasourcefilters(){
		GridGson gg = new GridGson();
		RptDatasourceFilter t = new RptDatasourceFilter();
		List<RptDatasourceFilter> items = t.find("select * from rpt_datasource_filter where DATASOURCE_ID=?",this.getParaToInt("id"));
		gg.setTotal(items.size());
		gg.setRows(_wrapRptDatasourceFilterJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewrptdatasourcefilterJson(){
		
		RptDatasourceFilter item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new RptDatasourceFilter();
		}else{
			Integer id = this.getParaToInt("id");
			item = new RptDatasourceFilter().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapRptDatasourceFilterJsonItem(item)));
	}
	
	public void updaterptdatasourcefilter(){
		Map t = getRequestObject(HashMap.class);
		RptDatasourceFilter item = null;
		if(t.containsKey("ID")){
			item = new RptDatasourceFilter().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new RptDatasourceFilter();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<RptDatasourceParamJson> _wrapRptDatasourceParamJson(List<RptDatasourceParam> rptdatasourceparams){
		List<RptDatasourceParamJson> jsonItems = new ArrayList<RptDatasourceParamJson>();
		for(RptDatasourceParam gt:rptdatasourceparams){
			jsonItems.add(_wrapRptDatasourceParamJsonItem(gt));
		}
		return jsonItems;
	}

	private RptDatasourceParamJson _wrapRptDatasourceParamJsonItem(RptDatasourceParam objItem){
		List<RptDatasourceParamJson> jsonItems = new ArrayList<RptDatasourceParamJson>();
		
			RptDatasourceParamJson jsonItem = new RptDatasourceParamJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setDATASOURCE_ID(objItem.getDatasourceId());
			jsonItem.setPARAM_ID(objItem.getParamId());
			jsonItem.setPARAM_NAME(objItem.getParamName());
			jsonItem.setPARAM_WHERE_SQL(objItem.getParamWhereSql());
		
		return jsonItem;
	}
	
	public void rptdatasourceparams(){
		GridGson gg = new GridGson();
		RptDatasourceParam t = new RptDatasourceParam();
		List<RptDatasourceParam> items = t.find("select * from rpt_datasource_param where DATASOURCE_ID=?",this.getParaToInt("id"));
		gg.setTotal(items.size());
		gg.setRows(_wrapRptDatasourceParamJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewrptdatasourceparamJson(){
		
		RptDatasourceParam item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new RptDatasourceParam();
		}else{
			Integer id = this.getParaToInt("id");
			item = new RptDatasourceParam().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapRptDatasourceParamJsonItem(item)));
	}
	
	public void updaterptdatasourceparam(){
		Map t = getRequestObject(HashMap.class);
		RptDatasourceParam item = null;
		if(t.containsKey("ID")){
			item = new RptDatasourceParam().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new RptDatasourceParam();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<RptFilterDefineJson> _wrapRptFilterDefineJson(List<RptFilterDefine> rptfilterdefines){
		List<RptFilterDefineJson> jsonItems = new ArrayList<RptFilterDefineJson>();
		for(RptFilterDefine gt:rptfilterdefines){
			jsonItems.add(_wrapRptFilterDefineJsonItem(gt));
		}
		return jsonItems;
	}

	private RptFilterDefineJson _wrapRptFilterDefineJsonItem(RptFilterDefine objItem){
		List<RptFilterDefineJson> jsonItems = new ArrayList<RptFilterDefineJson>();
		
			RptFilterDefineJson jsonItem = new RptFilterDefineJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setFILTER_CODE(objItem.getFilterCode());
			jsonItem.setFILTER_NAME(objItem.getFilterName());
			jsonItem.setFILTER_DATA_SQL(objItem.getFilterDataSql());
		
		return jsonItem;
	}
	
	public void rptfilterdefines(){
		GridGson gg = new GridGson();
		RptFilterDefine t = new RptFilterDefine();
		List<RptFilterDefine> items = t.find("select * from rpt_filter_define ");
		gg.setTotal(items.size());
		gg.setRows(_wrapRptFilterDefineJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewrptfilterdefineJson(){
		
		RptFilterDefine item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new RptFilterDefine();
		}else{
			Integer id = this.getParaToInt("id");
			item = new RptFilterDefine().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapRptFilterDefineJsonItem(item)));
	}
	
	public void updaterptfilterdefine(){
		Map t = getRequestObject(HashMap.class);
		RptFilterDefine item = null;
		if(t.containsKey("ID")){
			item = new RptFilterDefine().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new RptFilterDefine();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<RptParamDefineJson> _wrapRptParamDefineJson(List<RptParamDefine> rptparamdefines){
		List<RptParamDefineJson> jsonItems = new ArrayList<RptParamDefineJson>();
		for(RptParamDefine gt:rptparamdefines){
			jsonItems.add(_wrapRptParamDefineJsonItem(gt));
		}
		return jsonItems;
	}

	private RptParamDefineJson _wrapRptParamDefineJsonItem(RptParamDefine objItem){
		List<RptParamDefineJson> jsonItems = new ArrayList<RptParamDefineJson>();
		
			RptParamDefineJson jsonItem = new RptParamDefineJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setPARAM_NAME(objItem.getParamName());
			jsonItem.setPARAM_TYPE(objItem.getParamType());
			jsonItem.setPARAM_DATA_SQL(objItem.getParamDataSql());
		
		return jsonItem;
	}
	
	public void rptparamdefines(){
		GridGson gg = new GridGson();
		RptParamDefine t = new RptParamDefine();
		List<RptParamDefine> items = t.find("select * from rpt_param_define ");
		gg.setTotal(items.size());
		gg.setRows(_wrapRptParamDefineJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewrptparamdefineJson(){
		
		RptParamDefine item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new RptParamDefine();
		}else{
			Integer id = this.getParaToInt("id");
			item = new RptParamDefine().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapRptParamDefineJsonItem(item)));
	}
	
	public void updaterptparamdefine(){
		Map t = getRequestObject(HashMap.class);
		RptParamDefine item = null;
		if(t.containsKey("ID")){
			item = new RptParamDefine().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new RptParamDefine();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<RptReportJson> _wrapRptReportJson(List<RptReport> rptreports){
		List<RptReportJson> jsonItems = new ArrayList<RptReportJson>();
		for(RptReport gt:rptreports){
			jsonItems.add(_wrapRptReportJsonItem(gt));
		}
		return jsonItems;
	}

	private RptReportJson _wrapRptReportJsonItem(RptReport objItem){
		List<RptReportJson> jsonItems = new ArrayList<RptReportJson>();
		
			RptReportJson jsonItem = new RptReportJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setTEMPLATE_ID(objItem.getTemplateId());
			jsonItem.setREPORT_NAME(objItem.getReportName());
			jsonItem.setDESCRIPTION(objItem.getDESCRIPTION());
		
		return jsonItem;
	}
	
	public void rptreports(){
		GridGson gg = new GridGson();
		RptReport t = new RptReport();
		List<RptReport> items = t.find("select * from rpt_report ");
		gg.setTotal(items.size());
		gg.setRows(_wrapRptReportJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewrptreportJson(){
		
		RptReport item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new RptReport();
		}else{
			Integer id = this.getParaToInt("id");
			item = new RptReport().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapRptReportJsonItem(item)));
	}
	
	public void updaterptreport(){
		Map t = getRequestObject(HashMap.class);
		RptReport item = null;
		if(t.containsKey("ID")){
			item = new RptReport().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new RptReport();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<RptReportDatasourceJson> _wrapRptReportDatasourceJson(List<RptReportDatasource> rptreportdatasources){
		List<RptReportDatasourceJson> jsonItems = new ArrayList<RptReportDatasourceJson>();
		for(RptReportDatasource gt:rptreportdatasources){
			jsonItems.add(_wrapRptReportDatasourceJsonItem(gt));
		}
		return jsonItems;
	}

	private RptReportDatasourceJson _wrapRptReportDatasourceJsonItem(RptReportDatasource objItem){
		List<RptReportDatasourceJson> jsonItems = new ArrayList<RptReportDatasourceJson>();
		
			RptReportDatasourceJson jsonItem = new RptReportDatasourceJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setREPORT_ID(objItem.getReportId());
			jsonItem.setDATASOURCE_ID(objItem.getDatasourceId());
			jsonItem.setSITE(objItem.getSITE());
			jsonItem.setDESCRIPTION(objItem.getDESCRIPTION());
		
		return jsonItem;
	}
	
	public void rptreportdatasources(){
		GridGson gg = new GridGson();
		RptReportDatasource t = new RptReportDatasource();
		List<RptReportDatasource> items = t.find("select * from rpt_report_datasource where REPORT_ID=?",this.getParaToInt("id"));
		gg.setTotal(items.size());
		gg.setRows(_wrapRptReportDatasourceJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewrptreportdatasourceJson(){
		
		RptReportDatasource item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new RptReportDatasource();
		}else{
			Integer id = this.getParaToInt("id");
			item = new RptReportDatasource().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapRptReportDatasourceJsonItem(item)));
	}
	
	public void updaterptreportdatasource(){
		Map t = getRequestObject(HashMap.class);
		RptReportDatasource item = null;
		if(t.containsKey("ID")){
			item = new RptReportDatasource().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new RptReportDatasource();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<RptReportDatasourceFilterJson> _wrapRptReportDatasourceFilterJson(List<RptReportDatasourceFilter> rptreportdatasourcefilters){
		List<RptReportDatasourceFilterJson> jsonItems = new ArrayList<RptReportDatasourceFilterJson>();
		for(RptReportDatasourceFilter gt:rptreportdatasourcefilters){
			jsonItems.add(_wrapRptReportDatasourceFilterJsonItem(gt));
		}
		return jsonItems;
	}

	private RptReportDatasourceFilterJson _wrapRptReportDatasourceFilterJsonItem(RptReportDatasourceFilter objItem){
		List<RptReportDatasourceFilterJson> jsonItems = new ArrayList<RptReportDatasourceFilterJson>();
		
			RptReportDatasourceFilterJson jsonItem = new RptReportDatasourceFilterJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setREPORT_ID(objItem.getReportId());
			jsonItem.setDATASOURCE_ID(objItem.getDatasourceId());
			jsonItem.setFILTER_ID(objItem.getFilterId());
			jsonItem.setFILTERS(objItem.getFILTERS());
		
		return jsonItem;
	}
	
	public void rptreportdatasourcefilters(){
		GridGson gg = new GridGson();
		RptReportDatasourceFilter t = new RptReportDatasourceFilter();
		List<RptReportDatasourceFilter> items = t.find("select * from rpt_report_datasource_filter where DATASOURCE_ID=?",this.getParaToInt("id"));
		gg.setTotal(items.size());
		gg.setRows(_wrapRptReportDatasourceFilterJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewrptreportdatasourcefilterJson(){
		
		RptReportDatasourceFilter item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new RptReportDatasourceFilter();
		}else{
			Integer id = this.getParaToInt("id");
			item = new RptReportDatasourceFilter().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapRptReportDatasourceFilterJsonItem(item)));
	}
	
	public void updaterptreportdatasourcefilter(){
		Map t = getRequestObject(HashMap.class);
		RptReportDatasourceFilter item = null;
		if(t.containsKey("ID")){
			item = new RptReportDatasourceFilter().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new RptReportDatasourceFilter();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}
	private List<RptTemplateJson> _wrapRptTemplateJson(List<RptTemplate> rpttemplates){
		List<RptTemplateJson> jsonItems = new ArrayList<RptTemplateJson>();
		for(RptTemplate gt:rpttemplates){
			jsonItems.add(_wrapRptTemplateJsonItem(gt));
		}
		return jsonItems;
	}

	private RptTemplateJson _wrapRptTemplateJsonItem(RptTemplate objItem){
		List<RptTemplateJson> jsonItems = new ArrayList<RptTemplateJson>();
		
			RptTemplateJson jsonItem = new RptTemplateJson();
			jsonItem.setID(objItem.getID());
			jsonItem.setNAME(objItem.getNAME());
			jsonItem.setTHUMB(objItem.getTHUMB());
			jsonItem.setCONTENT(objItem.getCONTENT());
			jsonItem.setSITES(objItem.getSITES());
		
		return jsonItem;
	}
	
	public void rpttemplates(){
		GridGson gg = new GridGson();
		RptTemplate t = new RptTemplate();
		List<RptTemplate> items = t.find("select * from rpt_template ");
		gg.setTotal(items.size());
		gg.setRows(_wrapRptTemplateJson(items));
		String datas = gson.toJson(gg);
		this.renderText(datas);
	}
	public void viewrpttemplateJson(){
		
		RptTemplate item = null;
		if(StrKit.isBlank(this.getPara("id"))){
			item = new RptTemplate();
		}else{
			Integer id = this.getParaToInt("id");
			item = new RptTemplate().findById(id);
		}
		
		this.renderText(gson.toJson(_wrapRptTemplateJsonItem(item)));
	}
	
	public void updaterpttemplate(){
		Map t = getRequestObject(HashMap.class);
		RptTemplate item = null;
		if(t.containsKey("ID")){
			item = new RptTemplate().findById(t.get("ID"));
			item._setAttrs(t);
			item.update();
		}else{
			item = new RptTemplate();
			item._setAttrs(t);
			item.save();
		}
		this.renderText(gson.toJson(ResponseJson.SuccessResult()));
	}

	/**
	 * 取Request中的数据对象
	 * @param valueType
	 * @return
	 * @throws Exception 
	 */
	protected <T> T getRequestObject(Class<T> valueType){
		try{
			StringBuilder json = new StringBuilder();
			BufferedReader reader = this.getRequest().getReader();
			String line = null;
			while((line = reader.readLine()) != null){
				json.append(line);
			}
			String jsonString = json.toString();
			reader.close();
			if(StrKit.isBlank(jsonString)){
				//sendResponseError("参数提交格式错误");
				return null;
			}
			Gson gson = new Gson();
			return gson.fromJson(jsonString, valueType);
		}catch(Exception ex){
			//sendResponseError("参数提交格式错误");
			return null;
		}
	}
}
